package com.ruoyi.web.controller.data;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.domain.DataUserRelation;
import com.ruoyi.data.service.IDataUserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 会员关系Controller
 *
 * @author zzp
 * @date 2022-10-18
 */
@RestController
@RequestMapping("/data/relation")
public class DataUserRelationController extends BaseController {
    @Autowired
    private IDataUserRelationService dataUserRelationService;

    /**
     * 查询会员关系列表
     */
    @PreAuthorize("@ss.hasPermi('data:relation:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataUserRelation dataUserRelation) {
        startPage();
        List<DataUserRelation> list = dataUserRelationService.selectDataUserRelationList(dataUserRelation);
        return getDataTable(list);
    }

    /**
     * 导出会员关系列表
     */
    @PreAuthorize("@ss.hasPermi('data:relation:export')")
    @Log(title = "会员关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataUserRelation dataUserRelation) {
        List<DataUserRelation> list = dataUserRelationService.selectDataUserRelationList(dataUserRelation);
        ExcelUtil<DataUserRelation> util = new ExcelUtil<DataUserRelation>(DataUserRelation.class);
        util.exportExcel(response, list, "会员关系数据");
    }

    /**
     * 获取会员关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('data:relation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dataUserRelationService.selectDataUserRelationById(id));
    }

    /**
     * 新增会员关系
     */
    @PreAuthorize("@ss.hasPermi('data:relation:add')")
    @Log(title = "会员关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataUserRelation dataUserRelation) {
        return toAjax(dataUserRelationService.insertDataUserRelation(dataUserRelation));
    }

    /**
     * 修改会员关系
     */
    @PreAuthorize("@ss.hasPermi('data:relation:edit')")
    @Log(title = "会员关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataUserRelation dataUserRelation) {
        return toAjax(dataUserRelationService.updateDataUserRelation(dataUserRelation));
    }

    /**
     * 删除会员关系
     */
    @PreAuthorize("@ss.hasPermi('data:relation:remove')")
    @Log(title = "会员关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dataUserRelationService.deleteDataUserRelationByIds(ids));
    }
}
